package test.enba.mq.producer.controller.rabbitmq.header;

import java.util.HashMap;
import java.util.Map;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HeadersExchangeConfig {

  // 定义头交换器
  @Bean
  public HeadersExchange headersExchange() {
    return new HeadersExchange("headers-exchange");
  }

  // 定义队列
  @Bean
  public Queue headersQueue() {
    return new Queue("headers-queue");
  }

  // 绑定队列到交换器，并指定头部匹配规则
  @Bean
  public Binding headersBinding(HeadersExchange headersExchange, Queue headersQueue) {
    Map<String, Object> headers = new HashMap<>();
    headers.put("type", "message");
    headers.put("format", "text");
    return BindingBuilder.bind(headersQueue).to(headersExchange).whereAll(headers).match();
  }
}
